package com.yanqu.road.server.cmd.cross.tradewar;

import com.yanqu.road.entity.config.battle.BattleResult;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.pb.tradewar.TradeWarProto;
import com.yanqu.road.pb.tradewar.team.TradeWarTeamProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.TradeWarModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;


@Cmd(code = Protocol.S_CROSS_TRADE_WAR_TO_ENEMY_BATTLE_RESP, desc = "跨服商战到对手区服战斗返回响应")
public class CrossTradeWarToEnemyBattleRespCmd extends AbstractCommand {
	private CrossTradeWarProto.CrossTradeWarToEnemyBattleRespMsg reqMsg;
	private BattleResult battleResult;
	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		reqMsg = CrossTradeWarProto.CrossTradeWarToEnemyBattleRespMsg.parseFrom(packet.getBytes());
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(reqMsg.getRet() == 0) {
			battleResult = player.getModule(TradeWarModule.class).dealCrossBattleResp(reqMsg);
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		TradeWarProto.TradeWarBattleStartRespMsg.Builder respMsg = TradeWarProto.TradeWarBattleStartRespMsg.newBuilder();
		respMsg.setRet(reqMsg.getRet());
		respMsg.setFastTeamNum(reqMsg.getFastTeamNum());
		if (0 == reqMsg.getRet()) {
			//战斗详细信息
			respMsg.setBattleDetailList(reqMsg.getBattleDetailList());
			//我方出战门客
			respMsg.addAllPatronsIdList(reqMsg.getPatronsIdListList());
			//对手出战门客
			respMsg.addAllEnemyPatronsList(reqMsg.getEnemyPatronsListList());
			respMsg.setKillPatronsCount(reqMsg.getKillPatronsCount());
			respMsg.setMyAddScore(reqMsg.getMyAddScore());
			respMsg.setEnemyLoseScore(reqMsg.getEnemyLoseScore());
			respMsg.setReward(battleResult.getReward());
			respMsg.setEffectMammonSkill(battleResult.getEffectMammonSkill());
			respMsg.setAddEarnSpeed(battleResult.getAddEarnSpeed().intValue());
			respMsg.setUseItemId(battleResult.getUseItemId());
			respMsg.setFastTeamNum(battleResult.getTeamIndex());
		}

		if (battleResult.getTeamIndex() == -1) {
			player.sendPacket(Protocol.U_CROSS_TRADE_WAR_BATTLE_START, respMsg);
		} else {
			CrossTradeWarProto.CrossTradeWarFastBattleRespMsg.Builder builder = CrossTradeWarProto.CrossTradeWarFastBattleRespMsg.newBuilder();
			builder.setRet(0);
			builder.setActivityId(reqMsg.getActivityId());
			builder.setTeamNum(battleResult.getTeamIndex());

			TradeWarTeamProto.TradeWarTeamBattleTempMsg.Builder resultMsg = TradeWarTeamProto.TradeWarTeamBattleTempMsg.newBuilder();
			resultMsg.setEnemyBaseData(reqMsg.getEnemyBaseInfo());
			resultMsg.setKillPatronsCount(reqMsg.getKillPatronsCount());
			resultMsg.setMyAddScore(reqMsg.getMyAddScore());
			resultMsg.setEnemyLoseScore(reqMsg.getEnemyLoseScore());
			resultMsg.setReward(battleResult.getReward());
			resultMsg.setEffectMammonSkill(battleResult.getEffectMammonSkill());
			resultMsg.setTeamIndex(battleResult.getTeamIndex());
			resultMsg.setBattleTimes(1);

			builder.setResultMsg(resultMsg);

			player.sendPacket(Protocol.U_CROSS_TRADE_WAR_FAST_BATTLE_START, builder);
		}
	}

}
